﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Logic;
using TrainingsApp.Model;
using ZJFramework10.Extension;
namespace TrainingsApp.IPADApp.Master {
    public partial class TeachMaterialsManager : PageBase {
        protected void Page_Load(object sender, EventArgs e) {

            cbxGrade.DataSource = TrainingsApp.Logic.TS_Grade_Logic.ListAll();
            cbxGrade.ValueField = "Idx";
            cbxGrade.ValueType = typeof(int);
            cbxGrade.TextField = "GradeName";
            cbxGrade.DataBind();
            cbxGrade.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem("-- 所有年级 --", -1));
            cbxCourse.DataSource = TrainingsApp.Logic.TS_CourseInfo_Logic.TS_CourseInfo_ListByPaging(100, 1).TS_CourseInfo_List;
            cbxCourse.ValueField = "Idx";
            cbxCourse.ValueType = typeof(int);
            cbxCourse.TextField = "CourseName";
            cbxCourse.DataBind();
            cbxCourse.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem("-- 所有学科 --", -1));
            if (!IsPostBack) {
                cbxCourse.SelectedIndex = 0;
                cbxGrade.SelectedIndex = 0;
            }
            DataBinds();
        }



        public void DataBinds() {


            List<TS_TeachMaterials_Entity> tS_TeachMaterials_LogicTS_TeachMaterials_ListAll = TS_TeachMaterials_Logic.TS_TeachMaterials_ListAll(); ;
            if (this.cbxGrade.SelectedIndex > 0) {
                int gval = this.cbxGrade.Value.AsInt();
                tS_TeachMaterials_LogicTS_TeachMaterials_ListAll = tS_TeachMaterials_LogicTS_TeachMaterials_ListAll.Where(u => u.Grade.Value == gval).ToList();
            }
            if (this.cbxCourse.SelectedIndex > 0) {
                int cval = this.cbxCourse.Value.AsInt();
                tS_TeachMaterials_LogicTS_TeachMaterials_ListAll = tS_TeachMaterials_LogicTS_TeachMaterials_ListAll.Where(u => u.CourseId == cval).ToList();
            }
            if (!string.IsNullOrEmpty(this.txtName.Text)) {
                tS_TeachMaterials_LogicTS_TeachMaterials_ListAll = tS_TeachMaterials_LogicTS_TeachMaterials_ListAll.Where(u => u.CourseName.Contains(this.txtName.Text)).ToList();
            }
            this.aspDataSource.DataSource = tS_TeachMaterials_LogicTS_TeachMaterials_ListAll;
            this.aspDataSource.DataBind();
        }

        protected void aspDataSource_CustomButtonCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonCallbackEventArgs e) {
            if (e.ButtonID == "btnNew") {
                Response.Redirect("TeachMaterialsEdit.aspx");
            }
            else if (e.ButtonID == "btnImport") {
                var model = this.aspDataSource.GetRow(e.VisibleIndex) as TS_TeachMaterials_Entity;
                Response.Redirect("TeachMaterialsImport.aspx?mid=" + model.Idx);
            }
            else {
                var model = this.aspDataSource.GetRow(e.VisibleIndex) as TS_TeachMaterials_Entity;
                Response.Redirect("TeachMaterialsEdit.aspx?mid=" + model.Idx);
            }

        }
    }
}